import { createRouter, createWebHistory, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import App from '@/App.vue'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { loading } from '@/utils/loading'
import useUserStore from '@/stores/modules/user'
import { PageEnum } from '@/enums/pageEnum'
// import { MenuEnum } from '@/enums/appEnums'
// import { isExternal } from '@/utils/validate'
// // 过滤路由所需要的数据
// export function filterAsyncRoutes(routes: any[], firstRoute = true) {
//   return routes.map((route) => {
//     const routeRecord = createRouteRecord(route, firstRoute)
//     if (route.children != null && route.children && route.children.length) {
//       routeRecord.children = filterAsyncRoutes(route.children, false)
//     }
//     return routeRecord
//   })
// }
// // 创建一条路由记录
// export function createRouteRecord(route: any, firstRoute: boolean): RouteRecordRaw {
//   //@ts-ignore
//   const routeRecord: RouteRecordRaw = {
//     path: isExternal(route.paths) ? route.paths : firstRoute ? `/${route.paths}` : route.paths,
//     name: Symbol(route.paths),
//     meta: {
//       hidden: !route.isShow,
//       keepAlive: !!route.isCache,
//       title: route.menuName,
//       perms: route.perms,
//       query: route.params,
//       icon: route.menuIcon,
//       type: route.menuType,
//       activeMenu: route.selected
//     }
//   }
//   switch (route.menuType) {
//     case MenuEnum.CATALOGUE:
//       routeRecord.component = firstRoute ? LAYOUT : RouterView
//       if (!route.children) {
//         routeRecord.component = RouterView
//       }
//       break
//     case MenuEnum.MENU:
//       routeRecord.component = loadRouteView(route.component)
//       break
//   }
//   return routeRecord
// }

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'admin',
    redirect: '/admin',
    component: App,
    children: [
      {
        path: 'admin',
        name: 'Admin',
        meta: { title: '后台管理' },
        component: () => import('@/views/Admin/Layout/Index.vue'),
        redirect: '/admin/dashboard',
        children: [
          {
            path: 'dashboard',
            name: '控制台',
            meta: {
              title: '控制台',
              breadcrumb: ['后台管理', '控制台']
            },
            component: () => import('@/views/Admin/Dashboard/Index.vue')
          },
          {
            path: 'product',
            name: 'product',
            meta: {
              title: '产品库管理',
              breadcrumb: ['后台管理', '产品库管理']
            },
            redirect: '/admin/product/classify',
            children: [
              {
                path: 'classify',
                name: 'ProductClassify',
                meta: {
                  title: '产品库',
                  breadcrumb: ['后台管理', '产品库管理', '产品分类']
                },
                component: () => import('@/views/admin/Product/Classify.vue')
              },
              {
                path: 'library',
                name: 'ProductLibrary',
                meta: {
                  title: '产品列表',
                  breadcrumb: ['后台管理', '产品库管理', '产品列表']
                },
                // product library
                component: () => import('@/views/admin/Product/Library.vue')
              }
            ]
          },
          {
            path: 'system',
            name: 'System',
            meta: {
              title: '系统管理',
              breadcrumb: ['后台管理', '系统管理']
            },
            // component: () => import('@/views/admin/enterprise/List.vue'),
            redirect: '/admin/system/menu',
            children: [
              {
                path: 'menu',
                name: 'Menu',
                meta: {
                  title: '菜单管理',
                  breadcrumb: ['后台管理', '系统管理', '菜单管理']
                },
                component: () => import('@/views/Admin/System/Menu/Index.vue')
              },
              {
                path: 'user',
                name: 'user',
                meta: {
                  title: '用户管理',
                  breadcrumb: ['后台管理', '系统管理', '用户列表']
                },
                redirect: '/admin/system/user/list',
                children: [
                  {
                    path: 'list',
                    name: 'userList',
                    meta: {
                      title: '用户列表',
                      breadcrumb: ['后台管理', '系统管理', '用户列表']
                    },
                    component: () => import('@/views/admin/System/User/List.vue')
                  },
                  {
                    path: 'profile',
                    name: 'Profile',
                    meta: {
                      title: '个人中心',
                      breadcrumb: ['后台管理', '系统管理', '个人中心']
                    },
                    component: () => import('@/views/admin/System/User/Profile/Index.vue')
                  }
                ]
              },
              {
                path: 'website',
                name: 'Website',
                meta: {
                  title: '网站信息',
                  breadcrumb: ['后台管理', '系统管理', '网站信息']
                },
                component: () => import('@/views/Admin/System/Website/Information.vue')
              },
              {
                path: 'notice',
                name: 'notice',
                meta: {
                  title: '通知公告',
                  breadcrumb: ['后台管理', '系统管理', '通知公告']
                },
                component: () => import('@/views/Admin/System/Notice/Index.vue')
              },
              {
                path: 'message',
                name: 'Message',
                meta: {
                  title: '留言管理',
                  breadcrumb: ['后台管理', '系统管理', '留言管理']
                },
                component: () => import('@/views/Admin/System/Message/Index.vue')
              },
              {
                path: 'blogroll',
                name: 'Blogroll',
                meta: {
                  title: '友情链接',
                  breadcrumb: ['后台管理', '系统管理', '友情链接']
                },
                component: () => import('@/views/Admin/System/Blogroll/Index.vue')
              }
            ]
          }
        ]
      }
    ]
  },
  {
    // 登录
    path: '/admin/login',
    name: 'Login',
    meta: { title: '登录' },
    component: () => import('@/views/Login/Index.vue')
  }
]

const router = createRouter({
  history: createWebHistory(), // 不带 # 号
  // history: createWebHashHistory(), // 带 # 号
  routes
})
/**
 * 配置进度条，不需要小圈圈
 */
NProgress.configure({ showSpinner: false })

const loginPath = PageEnum.LOGIN
const defaultPath = PageEnum.INDEX
// 免登录白名单
const whiteList: string[] = [PageEnum.LOGIN, PageEnum.ERROR_403]
// 路由跳转前的监听操作
router.beforeEach(async (to, from, next) => {
  // 修改title
  document.title = to.meta.title + ' - 无锡九五装饰设计工程有限公司'
  // 启动进度条
  NProgress.start()
  const userStore = useUserStore()
  const isUserAuthenticated = () => {
    return !userStore.token ? false : true
  }
  if (!isUserAuthenticated()) {
    // next({ path: loginPath, query: { redirect: to.fullPath } })
    next()
  } else {
    next()
  }
  await userStore.getMenu()

  next()
  NProgress.done()
})

/**
 * 进度条完成
 */
router.afterEach(() => {
  // if (window.existLoading) {
  loading.hide()
  // }
  NProgress.done()
})

export default router
